package com.wcgksoft.controller.valve;


import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.comm.StringUtils;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.valve.Valve;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.valve.ValveService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 *
 * 阀门管理
 *
 */
@Controller
@RequestMapping("system/valve")
public class ValveController {

    @Autowired
    ValveService valveService;
    @Autowired
    EnterpriseProjectService enterpriseProjectService;


    /**
     *
     * 阀门管理列表查询
     *
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(
                         @RequestParam(value = "departName",required = false)String departName,
                         @RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "limit", required = false) Integer limit) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Valve.class);
        //criteria.add(Restrictions.eq("departId", departId));

        if(StringUtils.isNotEmpty(departName)) {
            criteria.add(Restrictions.like("departName", departName, MatchMode.ANYWHERE));
        }

        List<Valve> list = valveService.findByConditions(criteria,page,limit);
        for (Valve valve : list) {
            valve.setDepart(enterpriseProjectService.findById(valve.getDepartid()));
        }
        return Layui.data(0, "查询成功", valveService.getTotalCount(), list);
    }



     /*
       查询项目
       */
    @ResponseBody
    @RequestMapping(value = "getDepart")
    public List<EnterpriseProject> getDepart() {
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        criteria.add(Restrictions.eq("parentId", -1));
        List<EnterpriseProject> list = enterpriseProjectService.findByConditions(criteria);
        return list;
    }

    /*
       查询片区
       */
    @ResponseBody
    @RequestMapping(value = "getArea")
    public List<EnterpriseProject> getArea(@RequestParam(value = "departid") Integer departid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        criteria.add(Restrictions.eq("parentId", departid));
        List<EnterpriseProject> list = enterpriseProjectService.findByConditions(criteria);
        return list;
    }




    //添加
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data", required = false) String data){
        JSONObject param = JSONObject.fromObject(data);
        Integer departid = param.getInt("departid");
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            //String id1 = String.valueOf(param.get("id"));
            //if(null==id1||id1.equals("")){
                Valve valve = (Valve) JSONObject.toBean(JSONObject.fromObject(data), Valve.class);


                valve.setDepartName(enterpriseProjectService.findById(departid).getProjectName());
                valveService.saveOrUpdate(valve);
                return Layui.data(0, "添加成功", 1, Layui.addList(valve));
            }

    }


    //删除
    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        Valve byId = valveService.findById(id);
        valveService.delete(byId);
        return Layui.data(1, "删除成功", 1, null);
    }


}
